package com.example.mq.mqserver.core;

import lombok.Data;

import java.io.Serializable;
import java.util.UUID;

@Data
public class Message implements Serializable {
    private BasicProperties basicProperties = new BasicProperties();
    private byte[] body;
    //位置偏移量,便于在文件中取出数据
    //采用[offsetBeg, offsetEnd)区间
    //这两个属性是帮助从文件中读取数据的,不需要进行序列化
    private transient long offsetBeg = 0;
    private transient long offsetEnd = 0;
    //表示此消息在文件中是否有效(采用逻辑删除)
    //0x00 表示无效， 0x01 表示有效
    private byte isValid = 0x01;

    //通过一个"工厂方法"来创建message对象,随机生成Id
    public static Message createMessageWithId(BasicProperties basicProperties, String routingKey, byte[] body){
        Message message = new Message();
        if(basicProperties != null){
            message.setBasicProperties(basicProperties);
        }
        message.getBasicProperties().setMessageId("M_" + UUID.randomUUID().toString());
        message.getBasicProperties().setRoutingKey(routingKey);
        message.setBody(body);
        //此处 offsetBeg,offsetEnd,isValid 作用在持久化操作
        //当进行持久化时,才需要赋值
        return message;
    }

    public String getMessageId() {
        return this.basicProperties.getMessageId();
    }

    public void setMessageId(String messageId) {
       this.basicProperties.setMessageId(messageId);
    }

    public String getRoutingKey() {
        return this.basicProperties.getRoutingKey();
    }

    public void setRoutingKey(String routingKey) {
        this.basicProperties.setRoutingKey(routingKey);
    }

    public boolean isDurable() {
        return this.basicProperties.isDurable();
    }

    public void setDurable(boolean durable) {
        this.basicProperties.setDurable(durable);
    }

}
